import { defineStore } from 'pinia';

interface GoodsItem {
  c_gcode: string;
  c_barcode: string;
  c_name: string;
  c_model: string;
  c_content: number;
  c_type: string;
  c_status: string;
  c_pro_status: string;
  c_sale_status: string;
  c_price: number;
  c_price_disc: number;
  c_number: number;
  c_today_sale: null;
  c_n_min: number | null;
  c_n_max: number | null;
  c_dnlmt_number: number;
  c_onway: number;
  c_to_ret: number;
  c_sn_perday_pro: number;
  c_dnlmt_day: number;
  c_sn_perday: number;
  c_n_min_order: null;
  c_opl_prohibit: string | null;
  c_week0: number;
  c_week1: number;
  c_week2: number;
  c_week3: number;
  c_week4: number;
  c_provider: string;
  c_provider_name: string;
  c_d_onway: null;
  c_n_input?: number; // 购买数量
}

interface ReceiptState {
  cartList: GoodsItem[];
  fromData: {
    c_dname: any[];
    md: Record<string, any>;
  };
  isBack: boolean;
  receiptType: string;  // 单据类型
  receiptConfig: {
    AddSource: string,
    BillType: string;
    MdCode: string;
    HasAuditAuthority: boolean,
    ModuleConfig: {
      IsModify: boolean;
      PrimaryKeysJSON: string;
      Sort_Index: number;
      c_alterable_help: null | string;
      c_app_portal: null | string;
      c_byname: null | string;
      c_client_type: string;
      c_cname: string;
      c_code: string;
      c_comment: string;
      c_create_dt: null | string;
      c_creater: null | string;
      c_d_page: string;
      c_file: string;
      c_function_type: string;
      c_icon_big: null | string;
      c_icon_small: null | string;
      c_ini_param: string;
      c_limit_comment: string;
      c_limit_desc: null | string;
      c_limit_type: string;
      c_manage_help: null | string;
      c_modifier: null | string;
      c_modify_dt: null | string;
      c_name: string;
      c_operate_help: null | string;
      c_origin_type: string;
      c_plan_comment: null | string;
      c_plan_dt: null | string;
      c_planner: null | string;
      c_server_name: null | string;
      c_status: string;
      c_version: null | string;
      module_name: string;
    } | null;
  };
}

export const useReceiptStore = defineStore('receipt', {
  state: (): ReceiptState => ({
    cartList: [],
    fromData: {
      c_dname: [], //部门
      md: {}//门店
    },
    isBack: false,//是否返回
    receiptType: '',//单据类型
    receiptConfig: {
      AddSource: '',           //新增流程入口来源标识  Goods为商品，Department为部门
      BillType: '',
      MdCode: '',
      HasAuditAuthority: false,
      ModuleConfig: null
    }
  }),

  getters: {
    // 获取购物车商品总数
    cartCount(): number {
      return this.cartList.length;
    },

    // 获取购物车商品总数量
    totalQtty(): number {
      return this.cartList.reduce((total, item) => total + (+item.c_n_input || 0), 0);
    }
  },

  actions: {
    // 添加商品到购物车
    addToCart(item: GoodsItem) {
      const existItemIndex = this.cartList.findIndex(goods => goods.c_gcode === item.c_gcode);
      if (existItemIndex > -1) {
        // 如果商品已存在,更新数量
        this.cartList[existItemIndex] = item;
      } else {
        // 如果商品不存在,添加到购物车
        this.cartList.push({
          ...item,
        });
      }
    },

    // 从购物车移除商品
    removeFromCart(c_gcode: string) {
      const index = this.cartList.findIndex(item => item.c_gcode === c_gcode);
      if (index > -1) {
        this.cartList.splice(index, 1);
      }
    },

    // 更新购物车商品数量
    updateCartItemQtty(goodItem: GoodsItem) {
      const itemIndex = this.cartList.findIndex(goods => goods.c_gcode === goodItem.c_gcode);
      this.cartList[itemIndex] = goodItem;
    },
    
    // 清空购物车
    clearCart() {
      this.cartList = [];
    },

    // 添加替换购物车数据的方法
    replaceCartList(newCartList: GoodsItem[]) {
      this.cartList = newCartList;
    },

    // 更新 fromData 中的数据
    updateFromData<K extends keyof ReceiptState['fromData']>(
      key: K,
      value: ReceiptState['fromData'][K],
      setBack?: boolean
    ) {
      this.fromData[key] = value;
      if (setBack !== undefined) {
        this.isBack = setBack;
      }
    },

    setIsBack(value: boolean) {
      this.isBack = value;
    },

    // 更新单据类型
    updateReceiptType(type: string) {
      this.receiptType = type;
    },

    // 更新整个 receiptConfig
    updateReceiptConfig(config: ReceiptState['receiptConfig']) {
      this.receiptConfig = config;
    },

    // 更新 receiptConfig 中的特定字段
    updateReceiptConfigField<K extends keyof ReceiptState['receiptConfig']>(
      key: K,
      value: ReceiptState['receiptConfig'][K]
    ) {
      this.receiptConfig[key] = value;
    },

    // 更新 ModuleConfig
    updateModuleConfig(moduleConfig: ReceiptState['receiptConfig']['ModuleConfig']) {
      this.receiptConfig.ModuleConfig = moduleConfig;
    }
  }
}); 